package org.vectrics.recruiter.struts.tag;

import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.DataException;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.user.Person;
import org.vectrics.user.PersonSearchCriteria;
import org.vectrics.user.PersonSearcher;
import org.vectrics.user.web.struts.UserSelectTag;

public class JobCandidateSelectTag extends UserSelectTag {
	public static final long serialVersionUID = 1619232422;
	
	private transient static Logger log = Logger.getLogger(JobCandidateSelectTag.class);
	private String jobId = null;
	PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
	private String lastName = null;
	private String firstName = null;
	public static String DESCRIPTION = "Select New Candidate for Job";	
	
	
	public JobCandidateSelectTag() {
		super();
		//HashSet roles = new HashSet();
		//roles.add("recruit-candidate");
		criteria.setPrivileges(null);
		Set domains = CoreServiceLocator.getPersonService()
			.findDomainsWithUserPrivilege(ThreadProperties.getPerson(), Person.PRIVILEGE_PERSON_VIEW);
		criteria.setDomains(domains);
	}

	
	protected PersonSearcher getPersonSearcher() {
		return(new PersonSearcher());
	}
	
	/**
	 * @return
	 */
	public String getJobId() {
		return jobId;
	}

	public String getDescription() {
		return(DESCRIPTION);
	}

	/**
	 * @param jobId
	 */
	public void setJobId(String jobId) {
		this.jobId = jobId;
	}


	public String getSelectHref(Person person) {
		String href = getContextPath() + "/admin/jobs/candidate/add.do?jobId=" 
					+ getJobId() + "&userId=" + person.getId();
		return(href);	
	}
	
	protected PersonSearchCriteria getUserCriteria() {
		criteria.setFirstName(firstName);
		criteria.setLastName(lastName);
		criteria.setNotEmployees(true);
		
		if (log.isDebugEnabled()) {
			log.debug("getCriteria() - lastName = " + lastName);
		}
		return(criteria);	
	}
	
	protected String makePageTopContent() {
		log.debug("makePageTopContent() called");
		StringBuffer html = new StringBuffer();
		Job job = null;
		try {
			job = getJob();
		} catch (DataException de) {
			log.error("DataException finding job", de);
		}
		if (job != null) {
			Table table = new Table();
			table.setWidth("300");
			Row row = table.addRow();
			Cell cell = row.addCell();
			cell.setWidth("40%");
			cell.addElement(TextHelper.getFont("stdFont", "Job Title"));
			
			cell = row.addCell();
			cell.setWidth("10%");
	
			cell = row.addCell();
			cell.setWidth("40%");
			cell.setClass("formBg1");
			cell.addElement(TextHelper.getFont("stdFont", job.getTitle()));
	
			
			row = table.addRow();
			cell = row.addCell();
			cell = row.addCell();
			cell = row.addCell();
	
			html.append(table.toString());
		}
		return(html.toString());
	}
	
	
	private Job getJob() throws DataException {
		log.debug("getJob()");
		Job job = null;
		try {
			job = RecruitServiceLocator.getJobProvider().findJob(new Long(getJobId()));
		}
		catch (Exception de) {
			log.error("DataException finding job", de);
			throw new DataException("DataException finding job", de);
		}
		if (job == null) {
			log.error("Could not find job with id = " + getJobId());
			throw new DataException("Could not find job with id = " + getJobId());
		}
		return(job);
	}
	
	/**
	 * @return
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}
